package com.cjk.service.Impl;

import com.cjk.mapper.ClazzMapper;
import com.cjk.pojo.Clazz;
import com.cjk.pojo.ClazzPageHelper;
import com.cjk.pojo.PageResult;
import com.cjk.service.ClazzService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ClazzServiceImpl implements ClazzService {

    @Autowired
    private ClazzMapper clazzMapper;

    /**
     * 分页查询
     * @param clazzPageHelper
     * @return
     */
    @Override
    public PageResult<Clazz> page(ClazzPageHelper clazzPageHelper) {
        PageHelper.startPage(clazzPageHelper.getPage(), clazzPageHelper.getPageSize());
        List<Clazz> clazzList = clazzMapper.pageQuery(clazzPageHelper);
        Page<Clazz> clazzPage = (Page<Clazz>) clazzList;
        return new PageResult<Clazz>(clazzPage.getTotal(), clazzPage.getResult());
    }

    /**
     * 根据id删除班级
     * @param id
     */
    @Override
    public void deleteById(Integer id) {
        clazzMapper.deleteById(id);
    }

    /**
     * 添加班级
     * @param clazz
     */
    @Override
    public void add(Clazz clazz) {
        clazz.setCreateTime(LocalDateTime.now());
        clazz.setUpdateTime(LocalDateTime.now());
        clazzMapper.add(clazz);
    }

    /**
     * 根据id查询班级
     * @param id
     * @return
     */
    @Override
    public Clazz findById(Integer id) {

        return clazzMapper.findById(id);
    }

    //修改班级
    @Override
    public void update(Clazz clazz) {
        clazz.setUpdateTime(LocalDateTime.now());
        clazzMapper.update(clazz);

    }

    /**
     * 查询所有班级
     * @return
     */
    @Override
    public List<Clazz> list() {
        return clazzMapper.list();
    }
}
